package demo5;
/*
 杨辉三角
 */

import java.util.ArrayList;
import java.util.List;

class Solution{
    public List<List<Integer>> generate(int numRows){
        List<List<Integer>> ret=new ArrayList<>();
        List<Integer> row=new ArrayList<>();
        row.add(1);
        ret.add(row);
        //下面从第二行开始
        for (int i = 1; i < numRows; i++) {
            List<Integer> curRow=new ArrayList<>();
            curRow.add(1);//新的一行第一个值
            //上一行
            List<Integer> preRow=ret.get(i-1);
            for (int j = 1; j <= i; j++) {
                curRow.add(preRow.get(j)+preRow.get(j-1));
            }
            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }
}
class Solution1 {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> row = new ArrayList<Integer>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    row.add(1);
                } else {
                    row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
                }
            }
            ret.add(row);
        }
        return ret;
    }
}


public class Test {
    public static void main(String[] args) {
        Solution1 solution1=new Solution1();
        System.out.println(solution1.generate(2));
        Solution solution=new Solution();
        System.out.println(solution.generate(2));
    }
}













